package com.fowo.api.sys.controller;

import com.fowo.api.common.model.R;
import com.fowo.api.sys.entity.SysDepartment;
import com.fowo.api.sys.model.SysDepartmentSearchParamPo;
import com.fowo.api.sys.model.SysDepartmentVo;
import com.fowo.api.sys.model.SysDeptUserVo;
import com.fowo.api.sys.service.SysDepartmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "部门")
@RequestMapping("/api/sys/department")
@RestController
public class SysDepartmentController {

    @Resource
    private SysDepartmentService sysDepartmentService;

    @GetMapping
    @Operation(summary = "搜索部门")
    public R<List<SysDepartmentVo>> search(SysDepartmentSearchParamPo search) {
        return R.data(() -> sysDepartmentService.search(search));
    }

    @GetMapping("tree")
    @Operation(summary = "获取部门树结构")
    public R<List<SysDepartmentVo>> getTree(SysDepartmentSearchParamPo search) {
        return R.data(() -> sysDepartmentService.searchTree(search));
    }

    @GetMapping("user/tree")
    @Operation(summary = "获取部门用户树结构")
    public R<List<SysDeptUserVo>> deptUserTree(SysDepartmentSearchParamPo search) {
        return R.data(() -> sysDepartmentService.deptUserTree(search));
    }

    @GetMapping("{id}")
    @Operation(summary = "获取单个部门信息")
    public R<SysDepartment> getById(@PathVariable Long id) {
        return R.data(() -> sysDepartmentService.getById(id));
    }

    @PostMapping
    @Operation(summary = "新增部门")
    public R create(@RequestBody @Validated SysDepartment sysDepartment) {
        return R.ok(() -> sysDepartmentService.create(sysDepartment));
    }

    @PutMapping
    @Operation(summary = "编辑部门")
    public R update(@RequestBody @Validated SysDepartment sysDepartment) {
        return R.ok(() -> sysDepartmentService.update(sysDepartment));
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除部门")
    public R delete(@PathVariable Long id) {
        return R.ok(() -> sysDepartmentService.delete(id));
    }
}
